package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.CalUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ---验证手机号
 */
public class VerifyPhoneActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private String titles;

    private TextView tv_phone;
    private EditText et_code;

    private TextView tv_get_code;

    private String Oldphone;
    private int time = 0;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    time--;
                    if (time == 0) {
                        tv_get_code.setText("获取验证码");
                    } else {
                        tv_get_code.setText("获取验证码" + time + "s");
                        mHandler.sendEmptyMessageDelayed(1, 1000);
                    }
                    break;

                default:
                    break;
            }
        }
    };

    private String inputCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_phone);

        initData();
        initTitle();
        initView();
    }

    private void initData() {
        titles = getIntent().getStringExtra("title");
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("验证手机");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private void initView() {
        findViewById(R.id.get_verification_code).setOnClickListener(this);
        findViewById(R.id.tv_payPassword_next).setOnClickListener(this);
        tv_get_code = (TextView) findViewById(R.id.get_verification_code);

        tv_phone = (TextView) findViewById(R.id.payPassword_bound_phone);
        et_code = (EditText) findViewById(R.id.verification_code);

        tv_phone.setText(MyApplication.getInstance().getUserBean().getPhone());
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //获取验证码
            case R.id.get_verification_code:
                if (time == 0) {
                    time = 60;
                    mHandler.sendEmptyMessage(1);
                    upload();
                } else {
                    MyToast("别着急！正在申请验证码中");
                }
                break;

            //下一步
            case R.id.tv_payPassword_next:
                inputCode = et_code.getText().toString();
                if (!"".equals(inputCode)) {
                    upchange();
                } else if ("".equals(inputCode)) {
                    MyToast("验证码不能为空");
                }
                break;

            default:
                break;
        }
    }

    private void upchange() {
        showDialog();
        Oldphone = tv_phone.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("code", inputCode);
        map.put("phone", Oldphone);
        XUtil.Post(Url.USER_CHECKCODE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: inputCode= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        Intent intent = new Intent(VerifyPhoneActivity.this, VerifyPhoneNextActivity.class);
                        intent.putExtra("type", titles);
                        intent.putExtra("phone", Oldphone);
                        intent.putExtra("code", inputCode);
                        startActivity(intent);
                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private static final String TAG = "VerifyPhoneActivity";

    private void upload() {
        showDialog();
        Oldphone = tv_phone.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("phone", Oldphone);
        map.put("type", "1");
        map.put("other", CalUtil.Cal());
        XUtil.Post(Url.PHONEEXISTSENDCODE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: Oldphone= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String message = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(message);
                    } else {
                        MyToast(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }
}
